package com.wbc.smartHome.web;

import com.github.pagehelper.PageInfo;
import com.wbc.smartHome.model.Commodity;
import com.wbc.smartHome.model.CommodityLabel;
import com.wbc.smartHome.model.CommodityType;
import com.wbc.smartHome.model.Result;
import com.wbc.smartHome.service.CommodityService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import java.io.IOException;
import java.util.List;


@RestController
@RequestMapping(path = "/api/commodityCtl")
/*热门商品存取*/
@Api(tags = "商品模块")
public class CommodityController {


    @Autowired
    private CommodityService commodityService;

    @ApiOperation("获取所有商品")
    @ApiResponses({
            @ApiResponse(code = 200 , message = "OK"),
            @ApiResponse(code = 500 , message = "系统异常")
    })
    @PostMapping(path = "getCommodity")
    public Result getCommodityList(@RequestBody Commodity commodity) {
        PageInfo<Commodity> commodityList = commodityService.getCommodityList(commodity);
        return new Result(200, "OK", commodityList);
    }

    @ApiOperation("保存商品信息")
    @ApiResponses({
            @ApiResponse(code = 200 , message = "OK"),
            @ApiResponse(code = 500 , message = "系统异常")
    })
    @PostMapping(path = "save")
    public Result save(@RequestBody Commodity commodity,@RequestHeader("adminToken") String adminToken) {
        if (commodity.getId()==null){
            commodityService.setCommodity(commodity,adminToken);
        }
        else {
            commodityService.updateCommodity(commodity,adminToken);
        }
        return new Result(200,"ok",null);
    }

    /*通过id查询商品详细信息并将标签查询出来封装进商品*/
    @ApiOperation("通过商品id获取商品详细信息")
    @ApiImplicitParam(name = "id",value = "商品id", required = true,type = "Integer")
    @ApiResponses({
            @ApiResponse(code = 200 , message = "OK"),
            @ApiResponse(code = 500 , message = "系统异常")
    })
    @GetMapping(path = "getCommodityById")
    public Result getCommodityById(@RequestParam("id") Integer id) {
        Commodity commodity =commodityService.getCommodityById(id);
        return new Result(200,"OK",commodity);
    }

    /*商品推荐*/
    @ApiOperation("商品推荐")
    @ApiResponses({
            @ApiResponse(code = 200 , message = "OK"),
            @ApiResponse(code = 500 , message = "系统异常")
    })
    @GetMapping(path = "getRecommendCommodity")
    public Result getRecommendCommodity() {
        List<Commodity> commodityList = commodityService.getRecommendCommodity();
        return new Result(200,"OK",commodityList);
    }

    /*特价商城*/
    @ApiOperation("特价商城")
    @ApiResponses({
            @ApiResponse(code = 200 , message = "OK"),
            @ApiResponse(code = 500 , message = "系统异常")
    })
    @GetMapping(path = "getSpecialOffer")
    public Result getSpecialOffer() {
        List<Commodity> commodityList = commodityService.getSpecialOffer();
        return new Result(200,"OK",commodityList);
    }

    /*按照类型分类查询*/
    @ApiOperation("按照类型分类查询")
    @ApiImplicitParam(name = "typeId",value = "类型id", required = true,type = "Integer")
    @ApiResponses({
            @ApiResponse(code = 200 , message = "OK"),
            @ApiResponse(code = 201 , message = "未能查询到商品信息"),
            @ApiResponse(code = 500 , message = "系统异常")
    })
    @GetMapping(path = "getCommodityListByTypeId")
    public Result getCommodityListByTypeId(Integer typeId) {
        List<Commodity> commodityList = commodityService.getCommodityListByTypeId(typeId);
        if (commodityList.size()==0){
            return new Result(201,"抱歉，未能查询到商品信息",null);
        }
        return new Result(200,"OK",commodityList);
    }

    /*通过标签id获取商品*/
    @ApiOperation("通过标签id获取商品详细信息")
    @ApiImplicitParam(name = "labelId",value = "标签id", required = true,type = "Integer")
    @ApiResponses({
            @ApiResponse(code = 200 , message = "OK"),
            @ApiResponse(code = 201,message = "未能查询到商品信息"),
            @ApiResponse(code = 500 , message = "系统异常")
    })
    @GetMapping(path = "getCommodityListByLabelId")
    public Result getCommodityListByLabelId(Integer labelId) {
        List<Commodity> commodityList = commodityService.getCommodityListByLabelId(labelId);
        if (commodityList.size()==0){
            return new Result(201,"抱歉，未能查询到商品信息",null);
        }
        return new Result(200,"OK",commodityList);
    }

    @ApiOperation("删除商品")
    @ApiResponses({
            @ApiResponse(code = 200 , message = "删除成功"),
            @ApiResponse(code = 500 , message = "系统异常")
    })
    @GetMapping(path = "delete")
    public Result delete(int id){
        commodityService.delete(id);
        Result result = new Result(200, "删除成功", null);
        return result;
    }

    @ApiOperation("上传商品封面图")
    @PostMapping("/uploadImg")
    public Result uploadImg(@RequestParam("file") CommonsMultipartFile commonsMultipartFile) throws IOException {
        Result result = commodityService.uploadImg(commonsMultipartFile);
        return  result;
    }

    @ApiOperation("删除图片")
    @GetMapping(path = "deleteImg")
    public Result deleteImg(String imgurl) throws IOException {
        commodityService.deleteImg(imgurl);
        return new Result(200,"删除成功",null);
    }

    @ApiOperation("获取商品类型")
    @GetMapping("/commodityType")
    public Result getCommodityType() {
        List<CommodityType> list =commodityService.getCommodityType();
        Result result = new Result(200,"OK",list);
        return result;
    }
    @ApiOperation("获取商品标签")
    @GetMapping("/commodityLabel")
    public Result getCommodityLabel() {
        List<CommodityLabel> list =commodityService.getCommodityLabel();
        Result result = new Result(200,"OK",list);
        return result;
    }


    @ApiOperation("轮播图")
    @ApiResponses({
            @ApiResponse(code = 200 , message = "查询成功"),
            @ApiResponse(code = 500 , message = "系统异常")
    })
    @GetMapping(path = "getCommodityPhoto")
    public Result getCommodityPhoto(){
        List<Commodity> list = commodityService.getCommodityPhoto();
        return new Result(200,"OK",list);
    }

    /*@ApiOperation("商品推荐")
    @ApiResponses({
            @ApiResponse(code = 200 , message = "查询成功"),
            @ApiResponse(code = 500 , message = "系统异常")
    })
    @GetMapping(path = "getRecommendCommodity")
    public Result getRecommendCommodity() {
        List<Commodity> list = commodityService.getRecommendCommodity();
        return new Result(200,"OK",list);
    }*/
}
